//
//  RGSearchUserListView.m
//  RedGrass
//
//  Created by wukong on 2019/2/16.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchUserListView.h"
#import "RGSearchUserListCell.h"
#import <MJRefresh.h>

@interface RGSearchUserListView()<UITableViewDelegate, UITableViewDataSource>


@property (strong, nonatomic) UITableView *tabelView;
@property (strong, nonatomic) MJRefreshAutoNormalFooter *freshFooter;
@property (strong, nonatomic) NSMutableArray *userList;


@end

@implementation RGSearchUserListView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.tabelView];
        [_tabelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}


#pragma mark - tableView Delegate

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_userList count];
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RGSearchUserListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RGSearchUserListCell" forIndexPath:indexPath];
    if([_userList count]){
        RGVideoUserInfoModel *model = _userList[indexPath.row];
        if(model){
            cell.model = model;
        }
        AA_WEAK_MAKE(self) weakSelf = self;
        [cell setFocusStatusChangeBlock:^{
            if([weakSelf.viewDelegate respondsToSelector:@selector(changeFocusStatusWithView:chosedIndexPath:)]){
                [weakSelf.viewDelegate changeFocusStatusWithView:weakSelf chosedIndexPath:indexPath];
            }
        }];
    }
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.viewDelegate respondsToSelector:@selector(cellChosedWithView:chosedIndexPath:)]){
        [self.viewDelegate cellChosedWithView:self chosedIndexPath:indexPath];
    }
}

#pragma mark - public
-(void) reloadDataWithVedioList:(NSMutableArray *)userList{
    _userList = userList;
    [_tabelView reloadData];
}



-(void) endFooterFresh{
    [_tabelView.mj_footer endRefreshing];
}

-(void) viewHaveNoMoreData{
    [_tabelView.mj_footer endRefreshingWithNoMoreData];
}

-(void) reloadChosedCellWithIndexPath:(NSIndexPath *)path{
    //刷新单个cell
    if(path.row < _userList.count){
        [_tabelView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationNone];
    }
}


#pragma mark - response

-(void)loadMoreUser{
    if([self.viewDelegate respondsToSelector:@selector(loadMoreDataWithView:)]){
        [self.viewDelegate loadMoreDataWithView:self];
    }
}


#pragma mark - getter

-(UITableView *)tabelView{
    if(!_tabelView){
        _tabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tabelView registerClass:[RGSearchUserListCell class] forCellReuseIdentifier:@"RGSearchUserListCell"];
        _tabelView.backgroundColor = [UIColor blackColor];
        _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabelView.mj_footer = self.freshFooter;
        _tabelView.dataSource = self;
        _tabelView.delegate = self;
    }
    return _tabelView;
}

-(MJRefreshAutoNormalFooter *)freshFooter{
    if(!_freshFooter){
        _freshFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUser)];
        _freshFooter.automaticallyChangeAlpha = YES;
        [_freshFooter setTitle:@"暂时没有更多了" forState:MJRefreshStateNoMoreData];
    }
    return _freshFooter;
}

@end
